package com.aios.action.settings.controller;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Locale;

import org.apache.log4j.Logger;

import com.aios.entity.unit.ConnectionEnum;
import com.aios.util.CheckConnection;

public class ControllerEditAction extends AbstractControllerAction {

	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(ControllerEditAction.class);

	private InputStream inputStream;
	private String url;
	private String port;

	public String execute() {

		setMode(Mode.EDIT);

		try {
			populateCameraType(Locale.JAPAN);
			this.controller = findById();

			if (actionError != null && !(actionError.equals(""))) {
				addActionError(actionError);
			}

		} catch (Exception e) {
			logger.error(e);
			addActionError(getText("error.user.notfound"));
		}

		return SUCCESS;
	}

	public String validateConnection() {
		/*if (port == null || port.equals("")) {
			port = "80";
		}*/
		if (url.equals("")) {
			return SUCCESS;
		}
		else if (url.indexOf("://")>0) {
			url = url.substring(url.indexOf("://")+3);
		}
		ConnectionEnum status = CheckConnection.check(url,
				Integer.parseInt(port));
		inputStream = new ByteArrayInputStream(status.getMessage().getBytes());

		return SUCCESS;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getPort() {
		return port;
	}

	public void setPort(String port) {
		this.port = port;
	}

}
